package com.wing.oauth2.filter;

import com.wing.common.constant.CabAuthCode;
import com.wing.common.constant.ClientId;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.oauth2.exception.CabAuthException;
import com.wing.oauth2.exception.LoginAuthException;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.UserAuthVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码过滤器
 *
 * @author
 */
//@Profile({"test","pro"})
@Component
public class LoginAuthFilter extends OncePerRequestFilter {

    @Lazy
    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;


    @Autowired
    private  UserClient userClient;


    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
//        logger.info(httpServletRequest.getRequestURI());
//        logger.info(httpServletRequest.getMethod());
//        logger.info(httpServletRequest.getRemoteHost());
        String clientId = httpServletRequest.getParameter("client_id");
        String userName=httpServletRequest.getParameter("username");

        if ("/oauth2/admin/user/login".equalsIgnoreCase(httpServletRequest.getRequestURI())
                && httpServletRequest.getMethod().equals("POST")
                && !"admin".equals(userName)) {

                if (StringUtils.isNotBlank(clientId)){
                    JsonResult<UserAuthVO> jsonResult= userClient.findUserByUsername(userName);
//                    if (jsonResult==null||JsonResult.Code.SUCCESS.getCode()!=jsonResult.getCode()||jsonResult.getData()==null){
//                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new CabAuthException("获取用户失败"));
//                        return;
//                    }
//                    if (ClientId.CLIENT_ID_WEB.equals(clientId)&&
//                            (jsonResult.getData().getCabAuthCodes()!=null&&!jsonResult.getData().getCabAuthCodes().contains(CabAuthCode.BACKSTAGE_AUTH_CODE))&&
//                            (jsonResult.getData().getWcabAuthCodes()!=null&&!jsonResult.getData().getWcabAuthCodes().contains(CabAuthCode.WCAB_BACKSTAGE_AUTH_CODE))){
//                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new LoginAuthException("该用户没有PC后台的权限，如果需要请配置用户角色拥有此权限!"));
//                        return;
//                    }

//                    if (ClientId.CLIENT_ID_WX.equals(clientId)&&jsonResult.getData().getCabAuthCodes()!=null&&!jsonResult.getData().getCabAuthCodes().contains(CabAuthCode.WX_APP_AUTH_CODE)){
//                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new LoginAuthException("该用户没有微信小程序的权限，如果需要请配置用户角色拥有此权限!"));
//                        return;
//                    }

//                    if (ClientId.CLIENT_ID_CAB_APP.equals(clientId)&&jsonResult.getData().getCabAuthCodes()!=null&&!jsonResult.getData().getCabAuthCodes().contains(CabAuthCode.CAB_AUTH_CODE)){
//                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new LoginAuthException("该用户没有柜机的权限，如果需要请配置用户角色拥有此权限!"));
//                        return;
//                    }

//                    if (ClientId.CLIENT_ID_WCAB_APP.equals(clientId)&&jsonResult.getData().getWcabAuthCodes()!=null&&!jsonResult.getData().getWcabAuthCodes().contains(CabAuthCode.WCAB_AUTH_CODE)){
//                        authenticationFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, new LoginAuthException("该用户没有柜机的权限，如果需要请配置用户角色拥有此权限!"));
//                        return;
//                    }
                }

        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }



}